package com.MarkToTop.main;

import com.MarkToTop.util.Constant;
import com.MarkToTop.util.GameUtil;

import java.io.*;

/**
 * MarkWang 版权所有 © Copyright 2020
 *
 * @CreateDate: Created in 2020-06-23 12:18
 * @Author: MarkToTop -(づ｡◕‿‿◕｡)づ
 * @Description:  记录游戏时间的类
 */
public class GameTime {
    private static final GameTime GAME_TIME = new GameTime();

    //出现悬浮障碍物的时间
    public static final int HOVER_Barrier_TIME = 10;
    //出现上下移动障碍物的时间
    public static final int MOVING_Barrier_TIME = 20;
    //游戏的状态
    private int timeState;
    //还没开始计时
    public static final int STAET_READY = 0;
    //开始计时
    public static final int STAET_RUN = 1;
    //结束计时
    public static final int STAET_OVER = 2;

    //游戏计时时间 --> 毫秒
    private long startTime,endTime;
    //历史最高纪录
    private long bestTime;

    //构造
    private GameTime(){
        timeState = STAET_READY;
        bestTime = -1;
        try {
            loadBestTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static GameTime getInstance(){
        return GAME_TIME;
    }
    /**
     * 加载历史最好成绩
     */
    private void loadBestTime() throws Exception{
        File file= new File(Constant.SCORE_FILE_PATH);
        if(file.exists()){
            DataInputStream dis = new DataInputStream(new FileInputStream(file));
            bestTime = dis.readLong();
            dis.close();
        }
    }

    /**
     * 保存最好成绩
     */
    public void saveBestTime(long time) throws Exception{
        File file= new File(Constant.SCORE_FILE_PATH);
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
        dos.writeLong(time);
        dos.close();
    }

    public long getBestTime() {
        return bestTime;
    }

    public int getTimeState() {
        return timeState;
    }

    public void setTimeState(int timeState) {
        this.timeState = timeState;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public void setEndTime(long endTime) {
        this.endTime = endTime;
    }

    public long getTime() {
        if (STAET_RUN == timeState){
            return System.currentTimeMillis()-startTime;
        }
        return endTime - startTime;
    }

    /**
     *开始计时方法
     */
    public void startTimeing(){
        startTime = System.currentTimeMillis();
        timeState = STAET_RUN;
    }
    /**
     *结束计时方法
     */
    public void endTiming(){
        endTime = System.currentTimeMillis();
        timeState = STAET_OVER;

        long time = getSecondTime();
        //System.out.println(bestTime/1000);
        if(bestTime < time) {
            //最好记录包含本次
            bestTime = time;
            try {
                saveBestTime(time);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     *是否准备就绪
     */
    public boolean isReady(){
        return timeState == STAET_READY;
    }
    /**
     * 游戏时间毫秒换算为秒
     * @return
     */
    public long getSecondTime(){
        return getTime()/1000;
    }
    /**
     * 是否正在计时
     * @return
     */
    public boolean isTimeing(){
        return timeState == STAET_RUN;
    }

    public void reset() {
        timeState = STAET_READY;
        startTime = 0;
        endTime = 0;
    }
}
